package com.zzk.www.service.impl;

import com.zzk.www.service.MsgSendService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by shengshan.tang on 2016/8/28.
 */
@Service
public class MsgSendServiceImpl implements MsgSendService {

    public void sendFeedbackErrorMsg(YTWebSocket ytWebSocket, String message, String code,String detailMsg) {
//        HbLogContextMgr.writeErrorMsg("im", "msg", ytWebSocket.getKey(), message, detailMsg);
        HbLogContextMgr.writeMasterMsg("im","msg",ytWebSocket.getKey(),new Date().getTime(),message,false,detailMsg);
        ResponseObject responseObject = ResponseObjectUtils.buildResObject();
        responseObject.setSuccess(false);
        responseObject.setLevel(AppConstant.ResponseLevel.ERROR);
        responseObject.setBizType(""+MsgBizType.feedback.getCode());
        responseObject.setCode(code);
        responseObject.setMessage(message);
        String json = JsonUtils.object2Json(responseObject);
        if (ytWebSocket.getWebSocket().getReadyState().name().equals(WebSocket.READYSTATE.OPEN.name())) {
            ytWebSocket.getWebSocket().send(json);
        }
    }

    public void sendFeedbackSuccessMsg(YTWebSocket ytWebSocket, String message,String code,String descMsg) {
        HbLogContextMgr.writeSuccesMsg("im", "msg", ytWebSocket.getKey(), message,descMsg);
        ResponseObject responseObject = ResponseObjectUtils.buildResObject();
        responseObject.setSuccess(true);
        responseObject.setLevel(AppConstant.ResponseLevel.INFO);
        responseObject.setBizType(""+MsgBizType.feedback.getCode());
        if (StringUtils.isEmpty(code)) {
            code = SystemConstant.ResponseCode.NORMAL;
        }
        responseObject.setCode(code);
        responseObject.setMessage(message);
        String json = JsonUtils.object2Json(responseObject);
        if (ytWebSocket.getWebSocket().getReadyState().name().equals(WebSocket.READYSTATE.OPEN.name())) {
            ytWebSocket.getWebSocket().send(json);
        }
    }

    public String sendMsg(YTWebSocket ytWebSocket, UserVo userVo, String message, boolean online) {
        MessageVo msgBean = JsonUtils.json2Object(message, MessageVo.class);
        if (userVo != null) {
            msgBean.setReceiveNickname(userVo.getNickname());
            msgBean.setReceiveIp(userVo.getClientIp());
            msgBean.setReceiveServerIp(userVo.getServerIp());
        }
        if (online) {
            ResponseObject responseObject = ResponseObjectUtils.buildResObject();
            responseObject.setBizType("" + MsgBizType.msg.getCode());
            msgBean.setReceiveTime(new Date().getTime());
            responseObject.setData(msgBean);
            String json = JsonUtils.object2Json(responseObject);
            if (ytWebSocket.getWebSocket().getReadyState().name().equals(WebSocket.READYSTATE.OPEN.name())) {
                ytWebSocket.getWebSocket().send(json);
            }
        }
        String batchNo = msgBean.getMsgId();
        String typeText = online ? "在线" : "离线";
        String title = msgBean.getReceiveNickname() + " 已接收到【" + typeText + "】消息";
        HbLogContextMgr.writeSuccesMsg("im", "msg", batchNo, title);
        return JsonUtils.object2Json(msgBean);
    }


    public void doSendMsg(YTWebSocket ytWebSocket,MessageVo msgBean, String message) {
        String batchNo = msgBean.getMsgId();
        String title = msgBean.getSendNickname() + " 已发送【" + msgBean.getMsg() + "】";
        HbLogContextMgr.writeMasterMsg("im", "msg", batchNo, new Date().getTime(), title, true, message);

        //通知用户已发送
        ResponseObject responseObject = ResponseObjectUtils.buildResObject();
        responseObject.setSuccess(true);
        responseObject.setLevel(AppConstant.ResponseLevel.INFO);
        responseObject.setBizType(""+MsgBizType.sendStatus.getCode());
        responseObject.setCode(SystemConstant.ResponseCode.NORMAL);
        responseObject.setData(msgBean.getMsgId());
        String json = JsonUtils.object2Json(responseObject);
        if (ytWebSocket.getWebSocket().getReadyState().name().equals(WebSocket.READYSTATE.OPEN.name())) {
            ytWebSocket.getWebSocket().send(json);
        }
    }

    public void sendFriendSyncMsg(YTWebSocket ytWebSocket, UserVo userVo, String message, boolean online) {
        FriendVo friendVo = JsonUtils.json2Object(message, FriendVo.class);
        if (userVo != null) {
            friendVo.setNickname(userVo.getNickname());
        }
        if (online) {
            ResponseObject responseObject = ResponseObjectUtils.buildResObject();
            responseObject.setBizType("" + MsgBizType.friendMsg.getCode());
            responseObject.setData(friendVo);
            String json = JsonUtils.object2Json(responseObject);
            if (ytWebSocket.getWebSocket().getReadyState().name().equals(WebSocket.READYSTATE.OPEN.name())) {
                ytWebSocket.getWebSocket().send(json);
            }
        }
        String batchNo = friendVo.getFriendUserId() + "_" + friendVo.getNickname();
        String typeText = online ? "在线" : "离线";
        String title = friendVo.getNickname() + " 已接收到好友同步【" + typeText + "】消息";
        HbLogContextMgr.writeMasterMsg("im", "msg", batchNo, new Date().getTime(), title, true, message);
//        HbLogContextMgr.writeSuccesMsg("im", "msg", batchNo, title);
    }

    public void doSendFriendSyncMsg(FriendVo friendVo, String message) {
        String batchNo = friendVo.getFriendUserId() + "_" + friendVo.getNickname();
        String title = friendVo.getNickname() + " 已发送好友同步消息";
        HbLogContextMgr.writeMasterMsg("im", "msg", batchNo, new Date().getTime(), title, true, message);
    }

}
